<?php
namespace app\admin\logic;

use app\admin\model\PolicyAgreement;
use app\common\server\UrlServer;
use think\Db;

class PolicyAgreementLogic
{

    public static function lists($get)
    {
        $article = Db::name('policy_agreement');
        $where = [];
        $where[] = ['delete_time', '=', '0'];
        if (isset($get['title']) && $get['title']) {
            $where[] = ['title', 'like', '%' . $get['title'] . '%'];
        }
        $count = $article->where($where)->count();
        $list = $article->where($where)->page($get['page'], $get['limit'])->order('id desc')->select();
        foreach ($list as &$item) {
            $item['image'] = UrlServer::getFileUrl($item['image']);
            $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
        }
        return ['count' => $count, 'lists' => $list];
    }


    public static function addArticle($post)
    {
        $article = new PolicyAgreement();

        $data = [
            'title' => $post['title'],
            'image' => $post['image'],
            'content' => $post['content'],
            'create_time' => time()
        ];
        return $article->save($data);
    }

    public static function editArticle($post)
    {
        $article = new PolicyAgreement();
        $data = [
            'title' => $post['title'],
            'image' => $post['image'],
            'content' => $post['content'],
            'update_time' => time()

        ];
        return $article->save($data, ['id' => $post['id'], 'delete_time' => 0]);
    }

    public static function getArticle($id)
    {
        $article = new PolicyAgreement();
        $detail = $article::get(['id' => $id, 'delete_time' => 0]);
        $detail['abs_image'] = UrlServer::getFileUrl($detail['image']);
        return $detail;
    }

}